Converting WAV File Sampling Rates
Tech Note: TN0279
Product: ActiveX
Version: All
Date Added: 2008-03-05
Issue
Before .wav files can be played using TDT System 3 devices, they must often be resampled. The sample rate of the file must match the sample rate of the device. CoolEdit or other audio editing programs can be used to resample and save .wav files. After the file is resampled, it can be played from RPvdsEx using a SourceFile component. MATLAB can also be used to resample .wav files in real- time just before they are played. The file data can then be written to an RPvdsEx buffer for playing. This is especially helpful if many files are being used.
Workaround
The example code below reads in a .wav file and returns resampled data that is ready to play out on the DAC. WriteTagV is used to load the data into the SerialBuf component of an RPvdsEx circuit. In the example circuit, the data is fed to the SerialBuf using the "data_in" parameter tag.
Using TDTRP with Matlab R2015b and above
wav_filename = 'c:\tmp\myfile.wav'; % put path of .wav file here
new_sample_rate = TDT.FS;
[data, sample_rate] = audioread([wav_path wav_filename]);
[p, q] = rat(sample_rate / new_sample_rate, 0.0001);
new_data = resample (data, q, p);
TDT.write('data_in', 0, new_data');
TDT.trg(1)
Using TDTRP with Matlab R2015a and below
wav_filename = 'c:\tmp\myfile.wav'; % put path of .wav file here
new_sample_rate = TDT.FS;
[data, sample_rate, bps] = wavread(wav_filename);
[p, q] = rat(sample_rate / new_sample_rate, 0.0001);
new_data = resample (data, q, p);
TDT.write('data_in', 0, new_data');
TDT.trg(1)
Using RPcoX with Matlab R2015b and above
wav_filename = 'c:\tmp\myfile.wav'; % put path of .wav file here
new_sample_rate = 24414.0625; % put sample rate of RP here
[data, sample_rate] = audioread(wav_filename);
[p, q] = rat(sample_rate / new_sample_rate, 0.0001);
new_data = resample (data, q, p);
RP.WriteTagV('data_in', 0, new_data');
RP.SoftTrg(1);
Using RPcoX with Matlab R2015a and below
wav_filename = 'c:\tmp\myfile.wav'; % put path of .wav file here
new_sample_rate = 24414.0625; % put sample rate of RP here
[data, sample_rate, bps] = wavread(wav_filename);
[p, q] = rat(sample_rate / new_sample_rate, 0.0001);
new_data = resample (data, q, p);
RP.WriteTagV('data_in', 0, new_data');
RP.SoftTrg(1);
Note
This example can also be applied to other types of data (e.g. 16-bit integers).